/* Author: David McCoy dave.mccoy@cospandesign.com
 *
 *     This file is part of DLCM\SubServ.
 *
 *  DLCM\SubServ is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DLCM\SubServ is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DLCM\SubServ.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * GraphWidget.java
 *
 * Used to graph out numerical values.
 * This widget would not have been possible without the work of
 * JChart2D created by:  Achim Westermann
 *
 * Created on Apr 5, 2010, 9:44:43 PM
 */

package com.cospandesign.gui.pubsub.widgets;

import pubsub.PubSubManager;
import dlcm.Member;
import dlcm.Structure;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.traces.Trace2DLtd;
import java.util.Calendar;
import java.util.HashMap;

public class GraphWidget extends javax.swing.JFrame implements Widget {

    String ChannelName;
    String MemberName;
    dlcm.Structure mStructure;
    dlcm.Member mMember;
    PubSubManager Parent;
    double time = 0;

    HashMap<String, ITrace2D> TraceMap;


    /** Creates new form GraphWidget */
    public GraphWidget() {
        initComponents();

        TraceMap = new HashMap<String, ITrace2D>();
        //Create a chart

        //Create an ITrace2D
        //Note that dynamic charts need limited amount of values
        //ITrace2D trace = new Trace2DLtd(200);
        //trace.setColor(Color.BLUE);
        //chart2D.addTrace(trace);


        //ADataCollector collector = new RandomDataCollectorTimeStamped(trace, 50);
        //collector.start();
    }

    // <editor-fold defaultstate="collapsed" desc="Static Functions">
    public static String GetWidgetName() {
        return "JChart2D Widget";
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Inherited Widget Functions">
    public void ReceiveMessage(Structure structure) {
        for (Member member : structure.getMemberList()){
            if (TraceMap.containsKey(member.Name)){
                Double data = null;
                if (member.Data.getClass().equals(Byte.class)){
                    data = new Double((Byte)member.Data);
                }
                else if (member.Data.getClass().equals(Short.class)){
                    data = new Double((Short)member.Data);
                }
                else if (member.Data.getClass().equals(Integer.class)){
                    data = new Double((Integer)member.Data);
                }
                else if (member.Data.getClass().equals(Long.class)){
                    data = new Double((Long)member.Data);
                }
                else if (member.Data.getClass().equals(Float.class)){
                    data = new Double((Float)member.Data);
                }
                else if (member.Data.getClass().equals(Double.class)){
                    data = new Double((Double)member.Data);
                }
                else {
                    continue;
                }
                //Double time = (Double) new Double (Calendar.getChannel().getTimeInMillis());
                TraceMap.get(member.Name).addPoint(time++, data);
            }
        }
    }

    public boolean CanUseMember(Member member) {
        if (member.Data.getClass().equals(Byte.class)){
            return true;
        }
        if (member.Data.getClass().equals(Short.class)){
            return true;
        }
        if (member.Data.getClass().equals(Integer.class)){
            return true;
        }
        if (member.Data.getClass().equals(Long.class)){
            return true;
        }
        if (member.Data.getClass().equals(Float.class)){
            return true;
        }
        if (member.Data.getClass().equals(Double.class)){
            return true;
        }
        return false;
    }

    public void SetupWidget(String channelName, String memberName, Structure structure, PubSubManager parent) {
        ChannelName = channelName;
        MemberName = memberName;
        mStructure = structure;
        mMember = mStructure.getMember(memberName);
        Parent = parent;

        for (Member member : structure.getMemberList()){
           if (member.Data.getClass().equals(Byte.class)){
           }
           else if (member.Data.getClass().equals(Short.class)){
           }
           else if (member.Data.getClass().equals(Integer.class)){
           }
           else if (member.Data.getClass().equals(Long.class)){
           }
           else if (member.Data.getClass().equals(Float.class)){
           }
           else if (member.Data.getClass().equals(Double.class)){
           }  
           else {
               continue;
           }
           ITrace2D trace = new Trace2DLtd(200);
           TraceMap.put(member.Name, trace);
           chart2D.addTrace(trace);
        }



        setTitle(ChannelName + ": " + memberName + " Graph Widget");
        setVisible(true);
    }

    public void setWidgetVisibility(boolean visible) {
        setVisible(visible);
    }
    // </editor-fold>


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chart2D = new info.monitorenter.gui.chart.Chart2D();

        setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(getClass().getResource("/com/cospandesign/resources/cospandesign.png")));

        chart2D.setName("chart2D"); // NOI18N

        javax.swing.GroupLayout chart2DLayout = new javax.swing.GroupLayout(chart2D);
        chart2D.setLayout(chart2DLayout);
        chart2DLayout.setHorizontalGroup(
            chart2DLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 436, Short.MAX_VALUE)
        );
        chart2DLayout.setVerticalGroup(
            chart2DLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 348, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chart2D, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chart2D, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GraphWidget().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private info.monitorenter.gui.chart.Chart2D chart2D;
    // End of variables declaration//GEN-END:variables

}
